var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    product: [{
      productSeriestwoobj: {
        name: '',
        id: ''
      },
      specificationsobj: {
        name: '',
        id: ''
      },
      productModelobj: {
        name: '',
        id: ''
      },
      productImage:[],
      number: ''
    }],




  },
  // 因为会改变原数组,所以进行深拷贝
  deepCopy(data) {

    if (data && typeof data === "object") {
      //针对函数的拷贝
      if (typeof data === "function") {
        let tempFunc = data.bind(null);
        tempFunc.prototype = deepCopy(data.prototype);
        return tempFunc;
      }

      switch (Object.prototype.toString.call(data)) {
        case "[object String]":
          return data.toString();
        case "[object Number]":
          return Number(data.toString());
        case "[object Boolean]":
          return new Boolean(data.toString());
        case "[object Date]":
          return new Date(data.getTime());
        case "[object Array]":
          var arr = [];
          for (let i = 0; i < data.length; i++) {
            arr[i] = this.deepCopy(data[i]);
          }
          return arr;

          //js自带对象或用户自定义类实例
        case "[object Object]":
          var obj = {};
          for (let key in data) {
            //会遍历原型链上的属性方法，可以用hasOwnProperty来控制 （obj.hasOwnProperty(prop)
            obj[key] = this.deepCopy(data[key]);
          }
          return obj;
      }
    } else {
      //string,number,bool,null,undefined,symbol
      return data;
    }
  },
  bindcheckout(e){
    let index=e.currentTarget.dataset.index
    let listindex=e.currentTarget.dataset.listindex
    let product=this.deepCopy(this.data.product)
    product[listindex].productImage[index].ischeck=! product[listindex].productImage[index].ischeck
    this.setData({
      product:product
    })
  },
  bindprivew(e){
    console.log(e)
    let listindex=e.currentTarget.dataset.listindex
    let index=e.currentTarget.dataset.listindex
    let productImage=this.deepCopy(this.data.product[listindex].productImage)
    productImage.forEach(item=>{
      item.image=this.data.Imageurl+item.image
    })
    let newproductImage=productImage.map(item=>item.image)
    console.log(newproductImage)
    let t=this
    wx.previewImage({
      urls: newproductImage,
      current:newproductImage[index],
    })
  },
  goserch(e) {
    let productindex=e.currentTarget.dataset.productindex
    console.log(e.currentTarget.dataset.productindex)
    wx.navigateTo({
      url: '/shopowner/pages/productserch/productserchy?productindex='+productindex,
    })
  },

  delproduct(e) {
let t=this
    wx.showModal({
      title: '提示',
      content: '是否要删除该该产品',
      confirmColor: "#D6000F",
      success(res) {
        if (res.confirm) {
          let index = e.currentTarget.dataset.listindex
          let product = t.data.product
          product.splice(index, 1)
          t.setData({
            product: product
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  addproducts() {
    let product = this.data.product
    let obj={
      productSeriestwoobj: {
        name: '',
        id: ''
      },
      specificationsobj: {
        name: '',
        id: ''
      },
      productModelobj: {
        name: '',
        id: ''
      },
      number: ''
    }
    product.push(obj)
    this.setData({
      product: product
    })
  },
  bindnumber(e) {
    console.log(e)
    let listindex = e.currentTarget.dataset.listindex
    let product = this.data.product
    product[listindex].number=e.detail.value
    this.setData({
      product: product
    })
  },
  // showisspecifications(e) {
  //   console.log(e)
 
  //   let productindex = e.detail.value
  //   let product = this.data.product
   

  // },

  bindspecifications(e) {
  let index=e.detail.value
    let listindex = e.currentTarget.dataset.listindex
    let product = this.data.product
 
    product[listindex].productSeriestwoobj={
      name: this.data.productSeries[index].name,
      id: this.data.productSeries[index].id
    }
    product[listindex].specificationsobj ={
      name:  '',
      id: ''
    }
    product[listindex].productModelobj={
      name: '',
      id: ''
    }
    product[listindex].productImage=[]
    product[listindex].productSpec=this.data.productSeries[index].productSpec
    this.setData({
      product: product,
    })

  },

  bindproductSeriestwo(e) {
    console.log(e)
    let index = e.detail.value
    let listindex = e.currentTarget.dataset.listindex
    let product = this.data.product
    product[listindex].specificationsobj={
      name:  product[listindex].productSpec[index].name,
      id:  product[listindex].productSpec[index].id
    }


  
    product[listindex].productModelobj={
      name: '',
      id: ''
    }

    product[listindex].productImage=[]
    product[listindex].productModel= product[listindex].productSpec[index].productModel
    product[listindex].newproductModel= product[listindex].productSpec[index].productModel.map(item=>item.model+item.name)
    this.setData({
      product: product
    })
  },
  bindproductModel(e) {

    let index =e.detail.value
    let listindex = e.currentTarget.dataset.listindex
    let product = this.data.product
    product[listindex].productModelobj = {
      name: product[listindex].productModel[index].model+product[listindex].productModel[index].name,
      id: product[listindex].productModel[index].id
    }
    product[listindex].productModel[index].productImage.forEach((item1, index1) => {
      item1.ischeck = true
    })
    product[listindex].productImage= product[listindex].productModel[index].productImage
    // product[listindex].isproductModel = false
     this.setData({
    product: product
  })
  },
  bindimage(e) {
    let index = e.currentTarget.dataset.index
    let listindex = e.currentTarget.dataset.listindex
    let product = this.data.product
    let productImage =  product[listindex].productImage
    productImage[index].ischeck = !productImageItem.ischeck
    this.setData({
      product: product
    })
  },
  next() {
    console.log()
    let  product=this.data.product
      if(product.some(item=>item.specificationsobj.id=='')){
        wx.showToast({
          title: '您有个产品没有选择规格',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      return
      }
      if(product.some(item=>item.productSeriestwoobj.id=='')){
        wx.showToast({
          title: '您有个产品没有选择系列',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      if(product.some(item=>item.productModelobj.id=='')){
        wx.showToast({
          title: '您有个产品没有选择型号',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      console.log(product.some(item=>item.number==''))
      if(product.some(item=>item.number=='')){
        wx.showToast({
          title: '您有个产品没有填写瓦片数量',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
        return
      }
   let ischeckout=false

   product.forEach(item=>{
     console.log(item.productImage.some(item1=>item1.ischeck==true))
     if(!item.productImage.some(item1=>item1.ischeck==true)){
      ischeckout=true
     }
   })
if(ischeckout==true){
  wx.showToast({
    title: '您有个产品没有选择产品图',
    duration: 2500, //持续的时间
    icon: 'none', //图标
  })
  return
}
  if(this.data.todo=='modify'){
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    prevPage.setData({
      product: this.data.product
    })
    wx.navigateBack({
      delta: 1,
    })
  }else{
    wx.navigateTo({
      url:'/shopowner/pages/designchart/designchart?product='+JSON.stringify(this.data.product)+"&&id="+this.data.id
    })
  }
  },

  // showproductSeriestwo(e) {
  //   let listindex = e.currentTarget.dataset.listindex
  //   let product = this.data.product
  //   if (product[listindex].specificationsobj.id == '') {
  //     wx.showToast({
  //       title: '请先选择规格后在选择系列',
  //       icon: "none"
  //     })
  //   } else {
  //     product[listindex].isproductSeriestwo = !  product[listindex].isproductSeriestwo 
  //     product[listindex].isspecifications = false
  //     product[listindex].isproductModel = false
    
  //     this.setData({
  //       product: product
  //     })
  //   }
  // },

  showproductModel(e) {
    let listindex = e.currentTarget.dataset.listindex
    let product = this.data.product
    if (product[listindex].productSeriestwoobj.id == '') {
      wx.showToast({
        title: '请先选择系列后再选择型号',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
    } else {
      product[listindex].isproductModel = !product[listindex].isproductModel 
      product[listindex].isproductSeriestwo = false
      product[listindex].isspecifications = false
     
      this.setData({
        product: product
      })
    }
  },

  // 选择产品
  getProductSpec() {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'getProductSpec',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        // userId: 1569562832576524288,
        name: t.data.name,
      },
      success: res => {
        console.log(res)
        t.setData({
          productSeries: res.data.productSeries,
   
        })
        wx.hideLoading();
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },

  // 处理数据

  dealWith() {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      id: options.id,
      Imageurl: app.globalData.Imageurl
    })
    this.getProductSpec()
    if (options.todo) {
      this.setData({
        todo: options.todo,
        product:JSON.parse(options.product)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})